package rbac.View.Main.Service.array;


import rbac.View.Main.DoMain.Dto.UserDto;
import rbac.View.Main.DoMain.Entity.User;
import rbac.View.Main.DoMain.Entity.Dept;
import rbac.View.Main.DoMain.Entity.Role;
import rbac.View.Main.DoMain.Vo.UserVo;
import rbac.View.Main.DoMain.Vo.Result;
import rbac.View.Main.Service.IDeptService;
import rbac.View.Main.Service.IRoleService;
import rbac.View.Main.Service.IUserService;

public class UserService extends  BaseService<User, UserDto, UserVo> implements IUserService {
    private final IDeptService deptservice;

    private final IRoleService roleservice;

    private static  IUserService singleton;

    private UserService() {
        this.deptservice = DeptService.getInstance();
        this.roleservice = RoleService.getInstance();
        data=new User[DATA_SIZE];
        temp=new UserVo[DATA_SIZE];
    }

    public static IUserService getInstance(){
        if(singleton==null){
            singleton=new UserService();
        }
        return singleton;
    }
    @Override
    public  boolean deptInUse(int deptId){
        boolean inUse=false;
        for(User user:data){
            if(user!=null&&user.deptId==deptId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public  boolean roleInUse(int roleId){
        boolean inUse=false;
        for(User user:data){
            if(user!=null&&user.roleId==roleId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }

    @Override
    UserVo convertToVo(User user) {
        String deptName="";
        Dept dept=deptservice.findById(user.deptId);
        if(dept!=null){
            deptName=dept.name;
        }
        String roleName="";
        Role role=roleservice.findById(user.roleId);
        if (role != null){
            roleName=role.name;
        }
        return new UserVo(user.getId(),user.username,user.name,user.mobile,user.email,deptName,
                user.deptId,user.roleId,roleName,user.enabled);
    }
    @Override
    User createEntity(UserDto dto, int id) {
        return new User(id,dto.username, dto.name, dto.mobile, dto.email, dto.deptId, dto.roleId);
    }

    @Override
    void updateEntity(UserDto dto, User user) {
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }

    @Override
    public  Result create(UserDto dto) {
        if (!deptservice.existsById(dto.deptId)){
            return Result.faliure("创建失败，不存在指定的部门");
        }
        if (!roleservice.existsById(dto.roleId)){
            return Result.faliure("创建失败，不存在指定的角色");
        }
        return super.create(dto);
    }

    @Override
    public  Result update(UserDto dto, int id) {
        if(!deptservice.existsById(dto.deptId)){
            return Result.faliure("创建失败，不存在指定的部门");
        }
        if(!roleservice.existsById(dto.roleId)){
            return Result.faliure("创建失败，不存在指定的角色");
        }
        return super.update(dto, id);
    }

    @Override
    public Result toggle(int id){
        if(existsById(id)){
            return Result.faliure("切换失败，不存在该数据");
        }
        data[id].enabled=!data[id].enabled;
        return Result.success(data[id].enabled?"启用成功":"停用成功",null);
    }
}
